package smartcampus.scjava.controller.forth;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import smartcampus.scjava.POJO.DO.LibrarySeat;
import smartcampus.scjava.POJO.DO.LibrarySeatReserve;
import smartcampus.scjava.POJO.DTO.SeatReserveDTO;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.service.ILibrarySeatReserveService;
import smartcampus.scjava.service.ILibrarySeatService;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 座位预定表 前端控制器
 * </p>
 *
 * @author xian
 * @since 2023-11-22
 */
@RestController
@RequestMapping("/library-seat-reserve")
public class LibrarySeatReserveController {

    @Resource
    private ILibrarySeatReserveService reserveService;

    @Resource
    private ILibrarySeatService seatService;

    @RequestMapping("/listPage")
    public ResultVO<?> listPage(Integer  pageNum, Integer pageSize, String word, String userId) {
        if (pageNum == null || pageSize == null) {
            return ResultVO.error("分页参数错误");
        }
        if (userId == null || userId.isEmpty()) {
            return ResultVO.error("userId不能为空");
        }
        Page<LibrarySeatReserve> page = new Page<>(pageNum,pageSize);
        QueryWrapper<LibrarySeatReserve> query = new QueryWrapper<>();
        query.lambda().eq(LibrarySeatReserve::getUserId,userId);
        if (StringUtils.hasText(word)) {
            query.lambda().or().like(LibrarySeatReserve::getSeatNo, word);
        }
        reserveService.page(page,query);

        return ResultVO.success(page);
    }

    @RequestMapping("/listReserveBySeat")
    public ResultVO<?> listReserveBySeat(String seatId) {


        List<Map<String, Object>> maps = reserveService.listReserveBySeat(seatId);

        return ResultVO.success("成功",maps);
    }





    @RequestMapping("/add")
    public ResultVO<?> add(@RequestBody SeatReserveDTO seatReserveDTO) {

        String timeString = seatReserveDTO.getTimeString();
        if (!StringUtils.hasText(timeString)) {
            return ResultVO.error("时间不能为空");
        }

        String[] times = timeString.split(",");
        LibrarySeatReserve seatReserve = new LibrarySeatReserve();
        if (times.length >= 1) {
            boolean check = reserveService.checkRepeatSeatReserve(seatReserveDTO.getSeatId(), times);
            if (!check) {
                return ResultVO.error("时间已被占用");
            }
            // times长度大于1时需要判断是否连续
            if (times.length > 1) {
                for (int i = 0; i < times.length - 1; i++) {
                    if (Integer.valueOf(times[i]) + 1 != Integer.valueOf(times[i + 1])) {
                        return ResultVO.error("时间不连续");
                    }
                }
            }

            LibrarySeat byId = seatService.getById(seatReserveDTO.getSeatId());
            if (byId == null) {
                return ResultVO.error("座位不存在！");
            }
            seatReserve.setSeatId(seatReserveDTO.getSeatId());
            seatReserve.setSeatNo(byId.getSeatNo());
            seatReserve.setUserId(seatReserveDTO.getUserId());
            seatReserve.setBeginTime(LocalDateTime.of(LocalDate.now(), LocalTime.of(Integer.parseInt(times[0]),0,0,0)));
            seatReserve.setEndTime(seatReserve.getBeginTime().plusHours(times.length));
            seatReserve.setReserveMinutes(times.length*60);
            seatReserve.setIsDelete(0);
            seatReserve.setCreateTime(LocalDateTime.now());
            seatReserve.setUpdateTime(LocalDateTime.now());
            reserveService.save(seatReserve);
        }

        return ResultVO.success("预约成功");
    }

    @RequestMapping("/delete")
    public ResultVO<?> delete(Integer reserveId) {
        reserveService.removeById(reserveId);
        return ResultVO.success("删除成功");
    }


}
